package com.juku2024.juku.viewmodel

import android.util.SparseArray
import androidx.lifecycle.MutableLiveData
import com.juku2024.juku.R
import com.juku2024.juku.base.BaseApplication
import com.juku2024.juku.model.CategorySearchModel
import com.juku2024.juku.model.CategorySearchResultModel
import com.juku2024.juku.network.api.Api
import com.juku2024.juku.network.retrofit.OnRetrofitResultListener
import com.juku2024.juku.network.retrofit.RetrofitUtils
import com.juku2024.juku.request.CategoryContentRequest
import com.juku2024.juku.request.CategoryTagRequest
import com.juku2024.juku.tools.AppUtils
import com.juku2024.juku.tools.MD5Utils
import kotlinx.coroutines.*
import java.util.*

class VideoListViewModel : BaseViewModel() {

    private val tagListLiveData = MutableLiveData<SparseArray<CategorySearchModel>>()
    private val searchResultsLiveData = MutableLiveData<HashMap<String, CategorySearchResultModel>>()

    fun getSearchTagList(genreId: Int, callback: OnRetrofitResultListener<CategorySearchModel>) {
        doBaseHttp({
            var result: CategorySearchModel? = null
            if (tagListLiveData.value != null) {
                result = tagListLiveData.value?.get(genreId, null)
                if (result?.data?.isNotEmpty() == true) {
                    callback.onSuccess(result)
                } else {
                    result = null
                }
            } else {
                tagListLiveData.value = SparseArray()
            }
            return@doBaseHttp result
        }, {
            // 查当前数据表是否有数据
            val appId = getAppId()
            val timestamp = getSecondTimestamp()
            val sign = MD5Utils.getMD5String(genreId, appId, getAppKey(), timestamp, getExtra())
            val request = CategoryTagRequest(genreId, appId, timestamp, sign)
            return@doBaseHttp RetrofitUtils.instance.getApi(Api::class.java).getSearchTag(genreId, getCommonRequest(request))
        }, object : OnRetrofitResultListener<CategorySearchModel> {
            override fun onSuccess(t: CategorySearchModel) {
                if (t.data.isNullOrEmpty()) {
                    if (isNotActive()) return
                    callback.onFailed(AppUtils.getString(R.string.error_net))
                    return
                }
                if (isNotActive()) return
                tagListLiveData.value?.put(genreId, t)
                callback.onSuccess(t)
            }

            override fun onFailed(msg: String) {
                if (isNotActive()) return
                callback.onFailed(msg)
            }
        })
    }


    /**
     * 查询搜索结果内容
     */
    fun getCategoryList(genreId: Int, feature: Int, year: Int, page: Int, callback: OnRetrofitResultListener<CategorySearchResultModel>) {
        // 取消之前的请求
        val keyWord = StringBuilder()
        keyWord.append(genreId)
        keyWord.append("_")
        keyWord.append(feature)
        keyWord.append("_")
        keyWord.append(year)
        keyWord.append("_")
        keyWord.append(page)
        val realKey = keyWord.toString()
        doBaseHttp({
            var result: CategorySearchResultModel? = null
            if (searchResultsLiveData.value != null) {
                result = searchResultsLiveData.value?.get(realKey)
                if (result != null) {
                    if (isNotActive()) return@doBaseHttp null
                    callback.onSuccess(result)
                }
            } else {
                searchResultsLiveData.value = HashMap()
            }
            return@doBaseHttp result
        }, {
            val appId = getAppId()
            val timestamp = getSecondTimestamp()
            val sign = MD5Utils.getMD5String(genreId, feature, year, page, 12, appId, getAppKey(), timestamp, getExtra())
            val request = CategoryContentRequest(genreId, feature, year, page, 12, appId, timestamp, sign)
            return@doBaseHttp RetrofitUtils.instance.getApi(Api::class.java).getCategoryList(genreId, getCommonRequest(request))
        }, object : OnRetrofitResultListener<CategorySearchResultModel> {
            override fun onSuccess(t: CategorySearchResultModel) {
                if (t.album == null) {
                    if (isNotActive()) return
                    callback.onFailed(AppUtils.getString(R.string.error_net))
                    return
                }
                if (isNotActive()) return
                searchResultsLiveData.value?.put(realKey, t)
                callback.onSuccess(t)
            }

            override fun onFailed(msg: String) {
                if (isNotActive()) return
                callback.onFailed(msg)
            }
        })
    }

    override fun onCleared() {
        super.onCleared()
        tagListLiveData.value?.clear()
        searchResultsLiveData.value?.clear()
    }
}